package com.jnsw.testkotlin;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

/**
 * 动画
 */
public class MySurfaceView extends SurfaceView implements Callback, Runnable {

    // 用于控制SurfaceView 的大小、格式等，并且主要用于监听SurfaceView 的状态
    private SurfaceHolder sfh;
    // 声明一个画布
    private Canvas canvas;
    // 声明一个线程
    private Thread th;
    // 线程消亡的标识符
    private boolean flag;
    private Paint paint;
    // 创建位图
    private Bitmap bmp = BitmapFactory.decodeResource(this.getResources(),
            R.drawable.wifi_all);
    // 记录当前播放帧
    private int currentFrame;

    public MySurfaceView(Context context) {
        super(context);
        // 实例SurfaceView
        sfh = this.getHolder();
        // 为SurfaceView添加状态监听
        sfh.addCallback(this);
        paint = new Paint();
        // 画笔无锯齿
        paint.setAntiAlias(true);

    }

    /**
     * SurfaceView 视图创建，响应此函数
     */
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        flag = true;
        // 实例线程
        th = new Thread(this);
        // 启动线程
        th.start();
    }

    /**
     * SurfaceView 视图状态发生改变时，响应此函数
     */
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                               int height) {

    }

    /**
     * SurfaceView 视图消亡时，响应此函数
     */
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        flag = false;
    }

    private int minW = bmp.getWidth() / 6;

    private void myDraw() {
        try {
            //居中点X轴坐标
            int cW = this.getWidth() / 2 - minW / 2;
            //居中点Y轴坐标
            int cH = this.getHeight() / 2 - bmp.getHeight() / 2;
            canvas = sfh.lockCanvas();
            if (canvas != null) {
                // 刷屏，画布白色
                canvas.drawColor(Color.WHITE);
                canvas.save();
                // 设置画布的可视区域，大小为每帧的大小，居中
                canvas.clipRect(cW, cH, cW + minW, cH + bmp.getHeight());
                // 绘制位图，居中
                canvas.drawBitmap(bmp, cW - currentFrame * minW, cH, paint);
//                System.out.println(currentFrame + "-=-=-=-=-=---" + (cW - currentFrame * minW) + "-----------------------------------" + cH);
                canvas.restore();
            }
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            if (canvas != null) {
                sfh.unlockCanvasAndPost(canvas);
            }
        }
    }

    @Override
    public void run() {
        while (flag) {
            long start = System.currentTimeMillis();
            myDraw();
            logic();
            long end = System.currentTimeMillis();
            try {
                if (end - start < 500) {
                    Thread.sleep(500 - (end - start));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 游戏逻辑
     */
    private void logic() {
        currentFrame++;
        // 当播放的当前帧大于并且等于帧数组时，重置当前帧为0
        if (currentFrame >= 6) {
            currentFrame = 0;
        }
    }

}

